package com.jxin.util.serialize.consts;

import com.google.common.collect.Maps;
import com.jxin.util.serialize.handler.ISerializeHandler;
import com.jxin.util.spi.ServiceLoaderUtil;

import java.util.Collection;
import java.util.HashMap;

/**
 * 序列化实现枚举类
 * @author Jxin
 * @version 1.0
 * @since 2019/10/21 21:45
 */

public enum SerializerHandlerEnum {
    /**ProtoStuff 解析器*/
    PROTO_STUFF("ProtoStuff", 0),
    ;

    /**序列化器名称*/
    private final String name;
    /**序列化器类型*/
    private final Integer type;


    /**根据type获取Clazz的map*/
    private static final HashMap<Integer/*type*/, ISerializeHandler> TYPE_MAP = Maps.newHashMap();

    SerializerHandlerEnum(String name, Integer type) {
        this.name = name;
        this.type = type;

    }
    static {
        // serializer init
        final Collection<ISerializeHandler> serializerList = ServiceLoaderUtil.loadAll(ISerializeHandler.class);
        for (final ISerializeHandler handler : serializerList) {
            TYPE_MAP.put(handler.type(), handler);
        }
    }

    /**
     * 获取序列化实现枚举类实例,根据type
     * @param  type 序列化类型
     * @return 序列化器 实现
     * @author Jxin
     */
    public static ISerializeHandler handlerOfType(int type){
        return TYPE_MAP.get(type);
    }

    public String getName() {
        return name;
    }

    public Integer getType() {
        return type;
    }

}
